//导入相关模块
import {Move} from "../../../gcg/libs/role/body/Move.js";//移动组件
import {Interval} from "../../../gcg/libs/timer/Interval.js";//任务管理器
import {Role} from "../../../gcg/libs/role/role/Role.js";//角色组件
import {Crash} from "../../../gcg/libs/crash/Crash.js";//碰撞组件
import {CompeteConstant} from "../../../gcg/libs/constants/CompeteConstant.js";
import {Foe} from "../../../gcg/libs/foe/foe/Foe.js";
import {Log} from "../../../gcg/libs/log/Log.js";
//对抗常量

//运行多个敌人，敌人一处碰撞，全部停止

!function () {
    //初始化友军对象
    let roleElement = $('#role');
    let role = new Role();
    role.compete = CompeteConstant.FRIEND;
    role.setNickname();//可选
    role.setDirection(roleElement, 'down')
    role.init(roleElement);


    //初始化敌军对象//可选
    let foe = new Foe();
    let foeClass = 'foe';
    foe.setClass(foeClass);
    foe.compete = CompeteConstant.FOE;
    foe.setStartSit([{top:100,left:5},{top:160,left:60},{top:25,left:250}]);//设置初始位置
    foe.createFoes(3);
    foe.setDescription('这是一个不会动的智障敌人');


    //初始化移动组件
    let move = new Move();
    move.init(roleElement);



    //初始化碰撞检测组件
    let crash = new Crash();
    crash.init();

    // //初始化任务管理器
    let interval = new Interval();
    interval.time = 20;//移动频率


    let toElements = null;
    let itv = interval.set(function () {
        try{
            //友军移动
            move.do(roleElement);

            //目标碰撞检测[友军碰撞敌军]
            toElements = $(foe.getClass());

            //实时碰撞检测
            crash.detect(roleElement, toElements, function () {
                interval.stop(itv);
            });

            //边界碰撞检测
            crash.detectBody(roleElement,function () {
                interval.stop(itv);
            });

        }catch (e) {
            Log.log(e);
            interval.stop(itv);
        }
    });



    let foeInterval = new Interval();
    foeInterval.time = 50;//移动频率

    let foeElements = null;
    let foeItv = foeInterval.set(function () {
        try{
            //目标碰撞检测[友军碰撞敌军]
            foeElements = $(foe.getClass());
            //敌军移动
            move.do(foeElements);
            //实时碰撞检测
            crash.detect(roleElement, foeElements, function () {
                foeInterval.stop(foeItv);
            });

            //边界碰撞检测
            crash.detectBody(foeElements,function () {
                foeInterval.stop(foeItv);
            });

        }catch (e) {
            Log.log(e);
            foeInterval.stop(foeItv);
        }
    });
}();